package com.chaodev.base.controller;

import com.chaodev.base.base.RedisDao;
import com.chaodev.base.model.Result;
import com.chaodev.base.model.ResultEnum;
import com.chaodev.base.model.UserInfo;
import com.chaodev.base.service.ReportService;
import com.chaodev.base.service.ILogService;
import com.chaodev.base.utils.FineDigitalSignature;
import com.chaodev.base.utils.JwtUtil;
import com.chaodev.base.utils.ParamDecrypt;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/report")
public class ReportController {

    @Autowired
    private RedisDao redisDao;

    @Autowired
    private ILogService logService;

    @Autowired
    private ReportService reportService;


    @GetMapping(value = "/getFineReport")
    @ResponseBody
    @ApiOperation(value = "获取帆软报表访问路径")
    @ApiImplicitParam(name = "token", paramType = "header", value = "token", dataType = "string", required = true)
    public Result getFineReport(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Result result = new Result();
        String token = request.getHeader("token");
        Map<String, Object> map = new HashMap<>();
        String p_menuid = request.getParameter("menuid");
        map.put("P_MENU_ID",p_menuid);
        UserInfo userInfo = new UserInfo();
        if (redisDao.existsKey(token)) {
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            List<Map<String, Object>> list = reportService.GetReportByMenuid(map);
            //配置的报表路径
            String reportPath=list.get(0).get("REPORT_PATH").toString();
            //截取帆软平台模板文件路径部分
            int startIndex=reportPath.indexOf('?')+1;
            int endIndex=(reportPath.contains("&")?reportPath.indexOf('&'):reportPath.length());
            String mbFilePath=reportPath.substring(startIndex,endIndex).replace("viewlet=","");
            //截取到的模板文件路径做两次URLDecode解码，用于生成数字签名（两次解码以防配报表时模板文件路径做了两次编码）
            String mbReport= URLDecoder.decode(mbFilePath,"UTF-8");
            mbReport= URLDecoder.decode(mbReport,"UTF-8");
            //生成数字签名
            String signature= FineDigitalSignature.createToken("", "", mbReport);
            String realReportPath = reportPath;//reportPath.replace(mbFilePath, mbReport);
            //参数加密
            String departid= ParamDecrypt.EncodeAES(userInfo.getDepartId());
            String userid= ParamDecrypt.EncodeAES(userInfo.getUserId());
            String roleid= ParamDecrypt.EncodeAES(userInfo.getRoleId());
            //最终返回的路径
            realReportPath=realReportPath + "&departid=" + departid + "&userid=" + userid + "&roleid=" + roleid + "&fine_digital_signature=" + signature;

            result.setData(realReportPath);
            result.setCode(ResultEnum.SUCCESS.getCode());

            Result logResult = new Result();
            logResult.setCode(ResultEnum.SUCCESS.getCode());
            logResult.setMsg("数据获取成功");//不记录返回数据，只做标识
            logResult.setServerID(result.getServerID());
            logService.logAdd("", "获取按钮类型", "SUCCESS", logResult.toString(), userInfo, request);
        } else {
            result.setCode(-1);
            result.setMsg("登录超时");
            logService.logAdd("", "获取按钮类型", "ERROR", result.toString(), userInfo, request);
        }
        return result;
    }
}
